// pages/examhome/examhome.js
const App = getApp();
const {
  get,
  post
} = App.require('request/index')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modifyList: [],
    catList: [{
      name: '模拟考试',
      id: 1
    }, {
      name: '正式考试',
      id: 2
    }],
    icon: 1,
    prolist: wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist')),
    navHeight: App.globalData.navHeight,
    currentPage: 1,
    pageSize: 10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getmodify();
  
  },

  check(e) {
    this.setData({
      icon: e.currentTarget.dataset.id
    })
    if (e.currentTarget.dataset.id == 2) {
      this.ksList()
    } else {
      this.getmodify()
    }
  },
  ksList() {
    let {
      stateList,
      currentPage,
      pageSize,
      prolist
    } = this.data
    post(`/app/exam/exam/list?currentPage=${currentPage}&currentPage${pageSize}`, {
      state: 0,
      examType: 2,
      resourceIds:[prolist.deptId,100]

    }).then(res => {
      if (res.data.list.length != 0) {
        let list = []
        list = res.data.list.map(item => {
          if (item.startTime || item.endTime) {
            return {
              ...item,
              startTime: item.startTime.slice(0, -3),
              endTime: item.endTime.slice(0, -3)
            }
          }
        })
        this.setData({
          stateList: list
        })
      }

    })
  },
  getmodify() {
    let {
      modifyList,
      currentPage,
      pageSize,
      prolist
    } = this.data
    post(`/app/exam/exam/list?currentPage=${currentPage}&currentPage${pageSize}`, {
      examType: 1,
      resourceIds:[prolist.deptId,100]
    }).then(res => {
      if (res.data.list.length != 0) {
        let list = []
        list = res.data.list.map(item => {
          if (item.startTime || item.endTime) {
            return {
              ...item,
              startTime: item.startTime.slice(0, -3),
              endTime: item.endTime.slice(0, -3)
            }
          }else{
            return {
              ...item
            }
          }
        })
        this.setData({
          modifyList: modifyList.concat(list)
        })
      }

    })
  },
  detail(e) {
  
    wx.navigateTo({
      url: `/pages/examadmin/pages/modifydetail/modifydetail?id=${e.target.dataset.id}`
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      currentPage: 1,
      modifyList: []
    })
    wx.showLoading({
      title: '刷新中...',
    })

    wx.hideLoading();
    wx.stopPullDownRefresh()
    this.getmodify()
    this.ksList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    this.setData({
      currentPage: this.data.currentPage + 1
    })
    this.getmodify()

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})